﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DreametryMIS
{
    public static class promtOutputDescription
    {
        public static string ShowDialog(String taskID)
        {
            String a = "";
            String b = "";
            Form prompt = new Form()
            {
                Width = 425,
                Height = 250,
                FormBorderStyle = FormBorderStyle.FixedDialog,
                BackColor = System.Drawing.Color.WhiteSmoke,
                Font = new Font("Segoe UI", 10, FontStyle.Regular),
                Text = "Output Description",
                StartPosition = FormStartPosition.CenterScreen,
                MaximizeBox = false,
                MinimizeBox = false
            };
            Label textLabel = new Label() { Left = 6, Top = 10, Text = "Name", Width = 120, Font = new Font("Segoe UI", 10, FontStyle.Regular), };
            TextBox name = new TextBox() { Left = 10, Top = 35, Width = 120, Height = 85, BorderStyle = BorderStyle.FixedSingle };
            Label textLabel2 = new Label() { Left = 136, Top = 10, Text = "Link", Width = 270, Font = new Font("Segoe UI", 10, FontStyle.Regular), };
            TextBox link = new TextBox() { Left = 140, Top = 35, Width = 270, Height = 85, BorderStyle = BorderStyle.FixedSingle };
            Button add = new Button()
            {
                Text = "Add",
                Left = 310,
                Width = 100,
                Height = 30,
                FlatStyle = FlatStyle.Flat,
                Top = 70,
            };

            Button confirmation = new Button()
            {
                Text = "Ok",
                Left = 310,
                Width = 100,
                Height = 30,
                FlatStyle = FlatStyle.Flat,
                Top = 110,
                Enabled = false
            };

            Button cancel = new Button()
            {
                Text = "Cancel",
                Left = 310,
                Width = 100,
                Height = 30,
                FlatStyle = FlatStyle.Flat,
                Top = 150
            };

            Label list = new Label() { Left = 0, Top = 0, Text = "", AutoSize = true, Font = new Font("Segoe UI", 9, FontStyle.Regular), };


            add.Click += (sender, e) => {
                if (name.Text == "" || link.Text == "")
                {
                    MessageBox.Show("Please fill all the fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    a = a + "<a href=" + link.Text + ">" + name.Text + "</a><br>";
                    b = b + name.Text + Environment.NewLine;
                    list.Text = b;
                    name.Text = "";
                    link.Text = "";
                }
                confirmation.Enabled = true;
            };

            confirmation.Click += (sender, e) => {
                if (MessageBox.Show(b+Environment.NewLine+"Are you sure you want to continue?", "Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    String commandText = "UPDATE TaskTable SET output_description = '" + a.Replace("'", "") + "' WHERE task_id = " + taskID;
                    using (SqlConnection con = new SqlConnection(ClassFlow.global_con))
                    {
                        SqlCommand com = new SqlCommand(commandText, con);
                        com.Connection.Open();
                        com.ExecuteNonQuery();
                    }
                    MessageBox.Show("The links are now added", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    prompt.Close();
                }
            };

            cancel.Click += (sender, e) => {
                a = "";
                prompt.Close();
            };

            Panel pnl = new Panel() {AutoScroll = true, Left = 10, Top = 70, Width = 290, Height = 140, BorderStyle = BorderStyle.None };

            pnl.Controls.Add(list);
            prompt.Controls.Add(pnl);
            prompt.Controls.Add(name);
            prompt.Controls.Add(link);
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(add);
            //prompt.Controls.Add(list);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(cancel);
            prompt.Controls.Add(textLabel2);
            //prompt.AcceptButton = confirmation;
    
            return prompt.ShowDialog() == DialogResult.OK ? a : a ;
            return prompt.ShowDialog() != DialogResult.OK ? "" : "";
        }

    }
}
